package com.travel.mapper.user;

import com.travel.model.entity.interaction.Favorite;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
//收藏mapper 查询收藏 插入收藏 
@Mapper
@Repository("favoriteMapperMyBatis")
public interface FavoriteMapper {
    
    @Select("SELECT * FROM favorites WHERE id = #{id}")
    @Results({
        @Result(property = "user", column = "user_id", 
            one = @One(select = "com.travel.mapper.user.UserMapper.findById")),
        @Result(property = "service", column = "service_id", 
            one = @One(select = "com.travel.mapper.service.ServiceMapper.findById"))
    })
    Optional<Favorite> findById(@Param("id") Long id);
    
    @Select("SELECT * FROM favorites WHERE user_id = #{userId}")
    List<Favorite> findByUserId(@Param("userId") Long userId);
    
    @Select("SELECT * FROM favorites WHERE user_id = #{userId} AND service_id = #{serviceId}")
    Optional<Favorite> findByUserIdAndServiceId(@Param("userId") Long userId, @Param("serviceId") Long serviceId);
    
    @Insert("INSERT INTO favorites (user_id, service_id, created_time) " +
           "VALUES (#{user.id}, #{service.id}, #{createdTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(Favorite favorite);
    
    @Delete("DELETE FROM favorites WHERE id = #{id}")
    int delete(Favorite favorite);
    
    @Delete("DELETE FROM favorites WHERE user_id = #{userId} AND service_id = #{serviceId}")
    int deleteByUserIdAndServiceId(@Param("userId") Long userId, @Param("serviceId") Long serviceId);
} 